package com.itheima.reggie.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.sql.visitor.functions.If;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private CartService cartService;
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void saveOrder(Orders orders) {
        User user = UserHolder.get();
        List<Cart> cartList = cartService.findCart();

        if (CollectionUtil.isEmpty(cartList)) {
            throw new CustomException("购物车为空");
        }
        Address address = addressMapper.selectById(orders.getAddressBookId());
        if(address==null){
            throw new CustomException("地址为空");
        }

        long ordersId = IdWorker.getId();
        BigDecimal amount = new BigDecimal(0);
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(ordersId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setImage(cart.getImage());
            orderDetailMapper.insert(orderDetail);
            amount = amount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
        }


        orders.setId(ordersId);//这个订单的主键是我们自己定义的
        orders.setNumber(String.valueOf(ordersId));//订单号 自己定义
        orders.setStatus(1);//1 表示待付款
        orders.setUserId(user.getId());//下单用户id
        orders.setOrderTime(new Date());//下单时间
        orders.setCheckoutTime(new Date());
        orders.setAmount(amount);//订单总金额 = 所有订单项金额之和
        orders.setUserName(user.getName());
        orders.setPhone(address.getPhone());//地址中得联系电话
        StringBuilder sb = new StringBuilder();//拼接具体地址
        sb.append(StringUtils.isNotEmpty(address.getProvinceName()) ? address.getProvinceName() : "");
        sb.append(StringUtils.isNotEmpty(address.getCityName()) ? address.getCityName() : "");
        sb.append(StringUtils.isNotEmpty(address.getDistrictName()) ? address.getDistrictName() : "");
        sb.append(StringUtils.isNotEmpty(address.getDetail()) ? address.getDetail() : "");
        orders.setAddress(sb.toString());//联系地址
        orders.setConsignee(address.getConsignee());//地址中取得的联系人名称
        ordersMapper.insert(orders);

        //注意: 企业中大多情况下,先一表,再来多表,因为多表中会用到一表的主键

        //6. 清空购物车
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartService.remove(wrapper);

    }
}
